
******MICROCREDIT OUTCOMES (TABLE 2; TABLE C.2 PANEL A; TABLE C.3 COLS 1 & 3)********

set more off
clear all 
clear matrix
capture log close


local seed = 999
local reps = 5000


//--> DATA: SUPERVISOR SUBJECTIVE EVALUATION 
	*********************************************************************************************************************************************************

	use "$data/CROSVY.dta", clear
	
	//--> DUMMIES FOR ABOVE AVG RATING (1= ABOVE; 2=AVG; 3=BELOW)
		
		g eval_disbursement = .
		replace eval_disbursement = 1 if sb2_1a==1
		replace eval_disbursement = 0 if sb2_1a==2|sb2_1a==3
		
		g eval_recovery = .
		replace eval_recovery = 1 if sb2_1b==1
		replace eval_recovery = 0 if sb2_1b==2|sb2_1b==3
		
		g eval_COmeet = .
		replace eval_COmeet = 1 if sb2_1c==1
		replace eval_COmeet = 0 if sb2_1c==2|sb2_1c==3		

		g eval_COsave = .
		replace eval_COsave = 1 if sb2_1d==1
		replace eval_COsave = 0 if sb2_1d==2|sb2_1d==3		
	
	g eval_cro = (eval_disbursement!=.)
	g eval_so = (eval_COmeet!=.)
	
	keep fw_code eval_disbursement eval_recovery eval_COmeet eval_COsave eval_cro eval_so
	sort fw_code
	tempfile temp_eval
	save `temp_eval'
	*********************************************************************************************************************************************************

			
//--> DATA: FA MONTH LEVEL ACTIVE LOANS AND RECOVERY (CREDIT OUTCOMES) 
	*********************************************************************************************************************************************************

		//--> FWCO PANEL: Maps each FAs CO portfolio
		
				use "$data/FWCO_panel.dta"
				
		//--> MERGE w/ CO Month Level MIS data		
				
				sort co_code month 
				merge co_code month using "$data/MIS_comthlevel.dta"
				drop _m
						
		//--> SOME COS are COMANAGED (SCALE THE ABSOLUTE NUMBERS BY COSHARE WEIGHT)	
			
				g weighted_num_active_loan = coshare_weight*num_active_loan

				
		//--> CALCULATE PERFORMANCE AT FA MONTH LEVEL
			*****************************************

				local var weighted_num_active_loan miss_pay_month num_pay
				collapse (sum) `var', by(fw_code month)
				
				//--> CALCULATE REPAYMENT RATES (RATIOS)
				
						g inv_ratio_miss_pay_month = 1 - (miss_pay_month/num_pay)
		
				keep fw_code month weighted_num_active_loan inv_ratio_miss_pay_month	
				
		//--> KEEP MONTHS OF CRO INTERVEIW
				
				keep if month==23 | month==24 | month==25 /* Apr, May and June 2006 to match CrO data */
				
		
				local varfin weighted_num_active_loan inv_ratio_miss_pay_month	
				reshape wide `varfin', i(fw_code) j(month)
				
					foreach var in `varfin' {
						rename `var'23 `var'_apr06 
						rename `var'24 `var'_may06 
						rename `var'25 `var'_jun06 
					}

		sort fw_code
		tempfile temp_credit
		save `temp_credit'
	*********************************************************************************************************************************************************

	
//--> DATA: FA MONTH LEVEL ATTENDANCE (SOCIAL OUTCOME) 
	*********************************************************************************************************************************************************
		
		//--> FWCO PANEL: Maps each FAs CO portfolio
		
				use "$data/FWCO_panel.dta"
				
				
		//--> MERGE w/ CO Month Level MPRS data		
				
				sort co_code month 
				merge co_code month using "$data/MPRS_comthlevel.dta"
				keep if _m==3
				drop _m
				
				//--> KEEP AUDITED DATA ONLY
				
					keep if cro_att_ ==1
					
					
		//--> CALCULATE PERFORMANCE AT FA MONTH LEVEL
			*****************************************
			
			local mprs_var num_mem_ curr_num_mem_
			collapse (sum) `mprs_var', by(fw_code month)
			
			
			//--> PERFORMANCE VARS
				
				g attendance_per_mem = num_mem_/curr_num_mem_    
				
				drop num_mem_ curr_num_mem_
				
			//--> KEEP MONTHS OF SO INTERVEIW
				
				keep if month==23 | month==24 | month==25 /* Apr, May and June 2006 to match CrO data */
				
		
				local varsoc attendance_per_mem
				reshape wide `varsoc', i(fw_code) j(month)
				
					foreach var in `varsoc' {
						rename `var'23 `var'_apr06 
						rename `var'24 `var'_may06 
						rename `var'25 `var'_jun06 
					}

		sort fw_code
		tempfile temp_social
		save `temp_social'
	*********************************************************************************************************************************************************

	//--> FEW FAS W/ MISSING VERIFIED MPRS DATA--> FOR THEM USE MPRS DATA 
		
	//--> DATA: FA MONTH LEVEL ATTENDANCE 
		*********************************************************************************************************************************************************
			
			//--> FWCO PANEL: Maps each FAs CO portfolio
			
					use "$data/FWCO_panel.dta"
					
					
			//--> MERGE w/ CO Month Level MPRS data		
					
					sort co_code month 
					merge co_code month using "$data/MPRS_comthlevel.dta"
					keep if _m==3
					drop _m
					
															
			//--> CALCULATE PERFORMANCE AT FA MONTH LEVEL
				*****************************************
				
				local mprs_var num_mem_ curr_num_mem_
				collapse (sum) `mprs_var', by(fw_code month)
				
				
				//--> PERFORMANCE VARS
					
					g attendance_per_mem = num_mem_/curr_num_mem_    
					
					drop num_mem_ curr_num_mem_
					
				//--> KEEP MONTHS OF SO INTERVEIW
					
					keep if month==23 | month==24 | month==25 /* Apr, May and June 2006 to match CrO data */
					
			
					local varsoc attendance_per_mem
					reshape wide `varsoc', i(fw_code) j(month)
					
						foreach var in `varsoc' {
							rename `var'23 `var'_apr06 
							rename `var'24 `var'_may06 
							rename `var'25 `var'_jun06 
						}

			sort fw_code
			tempfile temp_social_noaudit
			save `temp_social_noaudit'
			
		*********************************************************************************************************************************************************
				

	
	
		//--> DATA: SUPERVISOR EFFORT (KNOWLEDGE OF FA PERFORMANCE) 
				*********************************************************************************************************************************************************
			
			
			use "$data/CROSVY.dta", clear
			sort fw_code
			merge fw_code using `temp_credit', nok
			drop _m
			
			sort fw_code
			merge fw_code using `temp_social', nok
			drop _m
			
			sort fw_code
			merge fw_code using `temp_social_noaudit', nok update
			drop _m
			
			
			
			//--> DIFFERENCE IN SUPERVISOR RECALL AND ACTUAL 
				
				gen diff_num_active_loan=.
				replace diff_num_active_loan=abs(sb3_1-weighted_num_active_loan_apr06) if month==23
				replace diff_num_active_loan=abs(sb3_1-weighted_num_active_loan_may06) if month==24
				replace diff_num_active_loan=abs(sb3_1-weighted_num_active_loan_jun06) if month==25
												
				
				gen diff_recovery_rate=.
				replace diff_recovery_rate=abs(sb3_2-inv_ratio_miss_pay_month_apr06) if month==23
				replace diff_recovery_rate=abs(sb3_2-inv_ratio_miss_pay_month_may06) if month==24
				replace diff_recovery_rate=abs(sb3_2-inv_ratio_miss_pay_month_jun06) if month==25
				
				
				gen diff_attendance=.
				replace diff_attendance=abs(sb3_3-attendance_per_mem_apr06) if month==23
				replace diff_attendance=abs(sb3_3-attendance_per_mem_may06) if month==24
				replace diff_attendance=abs(sb3_3-attendance_per_mem_jun06) if month==25			
			
			
				//--> THRERE ARE 3 FAs w/ MISSING DATA--> REPLACE THEM WITH MEDIAN (RESULTS ARE ROBUST TO THIS CORRECTION)
				
			
					sort fw_code
					merge fw_code using "$data/FWHR.dta"
					drop _m
				
					foreach v in diff_num_active_loan diff_recovery_rate {
						sum `v' if treat==0 & sb3_1!=.,d
						local m = r(p50)
						replace `v' = `m' if `v'==. & sb3_1!=.
						}
					
					foreach v in diff_attendance {
						sum `v' if treat==0 & sb3_3!=.,d
						local m = r(p50)
						replace `v' = `m' if `v'==. & sb3_3!=.
						}					
				
			keep fw_code diff_num_active_loan diff_recovery_rate diff_attendance
			sort fw_code
			tempfile temp_effort
			save `temp_effort'
			
			
//--> COMBINE DATA (EVALUATION AND RECALL)
	*********************************************************************************************************************************************************
	
	use `temp_eval'
	sort fw_code
	merge fw_code using `temp_effort'
	drop _m
	
	sort fw_code
	save "$data/eval_fa_vars.dta", replace
